<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Price_Record extends ORM
{
    /**
     * @param $params
     * @param null $supplier_id
     * @param null $admin_id
     * @return false
     * @throws Kohana_Exception
     * 记录平台和供应商的变动信息
     */
    public static function add_record($params, $supplier_id=null, $admin_id=null)
    {
        if(!in_array($params['type_id'],array(1,2,3,5,114))){
            return false;
        }
        $table = St_Functions::table_field($params['type_id']);
        $product_id = DB::select()->from($table[0])->where("id={$params['suit_id']}")->execute()->get($table[1]);
        $data = array(
            'addtime'     => time(),
            'admin_id'    => $admin_id,
            'supplier_id' => $supplier_id,
            'type_id'     => $params['type_id'],
            'product_id'  => $product_id,
            'suit_id'     => $params['suit_id'],
            'describe'    => $params['describe'],
            'info'        => json_encode($params),
        );

        DB::insert('product_price_log', array_keys($data))->values(array_values($data))->execute();
    }

    /**
     * @param $params
     * 产品修改后 更改审核状态
     */
    public static function audit_status_update($params)
    {
        $table = St_Functions::table_field($params['type_id']);
        $field = "cfg_{$table[3]}_supplier_check_auto";
        $audit_type = DB::select()->from('sysconfig')->where("varname='{$field}'")->execute()->get('value');
        if($audit_type==0){
            if($params['type_id']==2){
                return false;
            }
            /*需要人工审核时 每次修改产品信息 都将状态改为待审核*/
            if($params['type_id']!=5){
                $product_id = DB::select()->from($table[0])->where("id={$params['suit_id']}")->execute()->get($table[1]);
                $product_info = DB::select()->from($table[3])->where("id={$product_id}")->execute()->current();
                if($product_info['status']>0){
                    DB::update($table[3])->set(array('status'=>1))->where("id={$product_id}")->execute();
                }
            }else{
                $product_info = DB::select()->from($table[0])->where("id={$params['suit_id']}")->execute()->current();
                if($product_info['status']>0){
                    DB::update($table[0])->set(array('status'=>1))->where("id={$product_info['id']}")->execute();
                }
            }
        }
    }
}